package com.firefightingiot.firefightingiot.web.service;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.firefightingiot.firefightingiot.common.base.BaseService;
import com.firefightingiot.firefightingiot.model.DeviceAlarmLog;
import com.firefightingiot.firefightingiot.model.DeviceBusiness;
import com.firefightingiot.firefightingiot.support.model.RespEntity;
import com.firefightingiot.firefightingiot.support.mybatisplus.QueryWrapper;
import com.firefightingiot.firefightingiot.support.utils.DayUtil;
import com.firefightingiot.firefightingiot.support.utils.requestutil.HttpRequestUtil;
import com.firefightingiot.firefightingiot.web.mapper.DeviceAlarmLogMapper;
import com.firefightingiot.firefightingiot.web.mapper.DeviceBusinessMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author Yang yu
 * @version 1.0
 * @date 2020/7/23 17:50
 */
@Service
@Slf4j
public class DeviceRunCountService extends BaseService {
    @Resource
    private DeviceBusinessMapper deviceBusinessMapper;
    @Resource
    private DeviceAlarmLogMapper deviceAlarmLogMapper;

    /**
     * 设备运行统计
     * @param jsonParam
     * @return
     */
    public Map<String, Object> deviceRunCount(JSONObject jsonParam){
        //处理支持form-data和json两种方式
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        //调用token验证接口
        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam, "systemsetup", "config", "getDecodeToken", "systemsetup");
        if (StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if (StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String homeId = token.getString("home_id");
        String userId = token.getString("user_id");
        String userType = token.getString("user_type");
        String businessId = null;

        if (userType.equals("1")) {
            businessId = jsonParam.getStr("businessId");
        } else {
            businessId = homeId;
        }
        //开始时间和结束时间
        if (StrUtil.isBlank(jsonParam.getStr("queryStarttime"))||StrUtil.isBlank(jsonParam.getStr("queryEndtime"))){
            jsonParam.put("queryStarttime", DayUtil.getMonthStart(new Date()));
            jsonParam.put("queryEndtime", DayUtil.getMonthEnd(new Date()));
        }
        //设备详情
        List<DeviceBusiness> deviceBusiness = deviceBusinessMapper.selectList(new QueryWrapper<DeviceBusiness>().eq("business_id",businessId));
        //单位详情
        JSONObject jsonParamN = new JSONObject();
        jsonParamN.put("id",businessId);
        com.alibaba.fastjson.JSONObject businessDetail = makeData(jsonParamN, "businessJava", "config", "getBusinessDetailNoToken", "business");
        //组装最终数据
        Map<String, Object> map = new HashMap<>();
        //单位名称
        map.put("businessName",businessDetail.getString("name"));
        //单位详细地址
        map.put("businessAddr",businessDetail.getString("addr"));
        //安装终端设备列表
        //ArrayList<String> deviceList = new HashSet<String>();
        HashSet<String> hashSet = new HashSet<>();
        for (DeviceBusiness device:deviceBusiness) {
            if (device.getDeviceClassify().equals("1")){
                //deviceList.add("智慧用电监测系统");
                hashSet.add("智慧用电监测系统");
            }
            if (device.getDeviceClassify().equals("2")){
                //deviceList.add("消防用水监测系统");
                hashSet.add("消防用水监测系统");
            }
            if (device.getDeviceClassify().equals("3")){
                //deviceList.add("智慧用电监测系统");
                hashSet.add("智慧型独立烟感");
            }
            if (device.getDeviceClassify().equals("6")){
                //deviceList.add("智慧用电监测系统");
                hashSet.add("隐患视频识别系统");
            }
        }
        //安全隐患巡查系统
        Integer checkAddr =  restEvaluation(jsonParam,"check","getCheckaddrCount","check");
        if (checkAddr>0){
            hashSet.add("安全隐患巡查系统");
        }
        //安装设备列表
        map.put("deviceList",hashSet);
        //--------------------------防火巡查---------------------------------
        //rest调用获取巡检巡查报告数据
        Map<String, Object> check =restCheck(jsonParam,"check","getCheckaddrForReportDoList","check");
        //巡查标签总数
        map.put("checkBQnum",check.get("allSize"));
        //本月巡查总数
        map.put("monthCheckNum",check.get("yesSize"));
        //未巡查总数
        map.put("NoCheck",check.get("noSize"));
        //巡查使用率
        map.put("checkRate",check.get("rate"));
        //本月隐患
        Integer monthHiddendanger =  restEvaluation(jsonParam,"check","getMonthHiddendanger","check");
        map.put("hiddendangerNum",monthHiddendanger);
        //本月已整改隐患
        Integer rectifyHiddendanger =  restEvaluation(jsonParam,"check","getRectifyHiddendanger","check");
        map.put("rectifyHiddendangerNum",rectifyHiddendanger);
        //--------------------------智慧用电监测---------------------------------
        //电设备总套数
        int electricityDeviceNum = deviceBusinessMapper.selectCount(new QueryWrapper<DeviceBusiness>().eq("business_id",businessId).eq("device_classify","1"));
        map.put("electricityDeviceNum",electricityDeviceNum);
        //电设备在线总数
        int electricityDeviceOnlineNum = deviceBusinessMapper.selectCount(new QueryWrapper<DeviceBusiness>().eq("business_id",businessId).eq("currentstate",1).eq("device_classify","1"));
        map.put("electricityDeviceOnlineNum",electricityDeviceOnlineNum);
        //电设备报警总数
        int electricityDeviceAlarmNum = deviceBusinessMapper.selectCount(new QueryWrapper<DeviceBusiness>().eq("business_id",businessId).eq("currentstate",2).eq("device_classify","1"));
        map.put("electricityDeviceAlarmNum",electricityDeviceAlarmNum);
        //电设备断线总数
        map.put("electricityDeviceDisconnectedNum",electricityDeviceNum-electricityDeviceOnlineNum);
        //电设备在线率
        map.put("electricityDeviceRate",electricityDeviceNum==0?0 + "%" : NumberUtil.roundStr(((double)electricityDeviceOnlineNum)*100/electricityDeviceNum,2) + "%");
        //报警原因
        JSONObject electricityAlarmTypeName = new JSONObject();
        electricityAlarmTypeName.put("businessId",businessId);
        electricityAlarmTypeName.put("deviceClassify","1");
        List<DeviceAlarmLog> electricitylogList = deviceAlarmLogMapper.getAlarmTypeName(electricityAlarmTypeName);
        String electricityAlarmName = "";
        for (DeviceAlarmLog str : electricitylogList){
            electricityAlarmName = str.getAlarmTypeName() + "," + electricityAlarmName;
        }
        map.put("electricityAlarmName",electricityAlarmName);
        //--------------------------建筑消防用水监测---------------------------------
        //水设备总套数
        int waterDeviceNum = deviceBusinessMapper.selectCount(new QueryWrapper<DeviceBusiness>().eq("business_id",businessId).eq("device_classify","2"));
        map.put("waterDeviceNum",waterDeviceNum);
        //水设备在线总数
        int waterDeviceOnlineNum = deviceBusinessMapper.selectCount(new QueryWrapper<DeviceBusiness>().eq("business_id",businessId).eq("currentstate",1).eq("device_classify","2"));
        map.put("waterDeviceOnlineNum",waterDeviceOnlineNum);
        //水设备报警总数
        int waterDeviceAlarmNum = deviceBusinessMapper.selectCount(new QueryWrapper<DeviceBusiness>().eq("business_id",businessId).eq("currentstate",2).eq("device_classify","2"));
        map.put("waterDeviceAlarmNum",waterDeviceAlarmNum);
        //电设备断线总数
        map.put("waterDeviceDisconnectedNum",waterDeviceNum-waterDeviceOnlineNum);
        //电设备在线率
        map.put("waterDeviceRate",waterDeviceNum==0?0 + "%" : NumberUtil.roundStr(((double)waterDeviceOnlineNum)*100/waterDeviceNum,2) + "%");
        //报警原因
        JSONObject waterAlarmTypeName = new JSONObject();
        waterAlarmTypeName.put("businessId",businessId);
        waterAlarmTypeName.put("deviceClassify","2");
        List<DeviceAlarmLog> waterlogList = deviceAlarmLogMapper.getAlarmTypeName(waterAlarmTypeName);
        String waterAlarmName = "";
        for (DeviceAlarmLog str : waterlogList){
            waterAlarmName = str.getAlarmTypeName() + "," + waterAlarmName;
        }
        map.put("waterAlarmName",waterAlarmName);
        //--------------------------慧眼识别---------------------------------
        //可视化设备总套数
        int visualizationDeviceNum = deviceBusinessMapper.selectCount(new QueryWrapper<DeviceBusiness>().eq("business_id",businessId).eq("device_classify","6"));
        map.put("visualizationDeviceNum",visualizationDeviceNum);
        //报警原因
        JSONObject visualizationAlarmTypeName = new JSONObject();
        visualizationAlarmTypeName.put("businessId",businessId);
        visualizationAlarmTypeName.put("deviceClassify","6");
        List<DeviceAlarmLog> visualizationlogList = deviceAlarmLogMapper.getAlarmTypeName(visualizationAlarmTypeName);
        String visualizationAlarmName = "";
        for (DeviceAlarmLog str : visualizationlogList){
            visualizationAlarmName = str.getAlarmTypeName() + "," + visualizationAlarmName;
        }
        map.put("visualizationAlarmName",visualizationAlarmName);
        //智慧用电本月报警日志
        electricityAlarmTypeName.put("queryStarttime",jsonParam.getStr("queryStarttime"));
        electricityAlarmTypeName.put("queryEndtime",jsonParam.getStr("queryEndtime"));
        List<DeviceAlarmLog> electricityMonthAlarmlog = deviceAlarmLogMapper.getMonthAlarmlog(electricityAlarmTypeName);
        map.put("electricityMonthAlarmlog",electricityMonthAlarmlog);
        //消防用水本月报警日志
        waterAlarmTypeName.put("queryStarttime",jsonParam.getStr("queryStarttime"));
        waterAlarmTypeName.put("queryEndtime",jsonParam.getStr("queryEndtime"));
        List<DeviceAlarmLog> waterMonthAlarmlog = deviceAlarmLogMapper.getMonthAlarmlog(waterAlarmTypeName);
        map.put("waterMonthAlarmlog",waterMonthAlarmlog);
        return map;
    }
    public List<Map<String, String>> getList(String title, String remark, List<Map<String, String>> list){
        Map<String, String> map = new HashMap<>();
        map.put("title",title);
        map.put("remark",remark);
        list.add(map);
        return list;
    }
}
